package com.hzhc.controller.baseinfo;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CompanyInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CompanyInfoService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CompanyInfoVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 客户信息 前端控制器
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/companyInfo")
public class CompanyInfoController extends BaseController {

    private final CompanyInfoService companyInfoService;

    /**
     * 客户分页查询
     * @param vo 查询字段
     */
    @RequestMapping("/getInfoList")
    public ResultUtil<List<CompanyInfoVo>> getInfoList(@RequestBody CompanyInfoVo vo) {
        Page<CompanyInfoVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<CompanyInfoVo> companyInfoVoList = companyInfoService.getList(vo,page);
        return ResultUtil.build(companyInfoVoList, page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 客户新增
     * @param info 客户属性值
     */
    @PostMapping("/addInfo")
    @SysTemLog(operModul = "商务管理-客户管理", operType = "新增", operDesc = "客户新增功能", modular = "PC")
    public ResultUtil<T> addInfo(@RequestBody CompanyInfo info, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        info.setUserId(user.getId());
        ResultUtil<T> res = companyInfoService.checkInfo(info);
        if (!(res.getCode() == 200)) {
            return res;
        }
        return companyInfoService.addRet(info);
    }

    /**
     * 客户编辑
     * @param info 客户属性值
     */
    @PostMapping("/updateInfo")
    @SysTemLog(operModul = "商务管理-客户管理", operType = "编辑", operDesc = "客户编辑功能", modular = "PC")
    public ResultUtil<T> updateInfo(@RequestBody CompanyInfo info,HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        info.setUpdateUserId(user.getId());
        ResultUtil<T> res = companyInfoService.checkInfo(info);
        if (!(res.getCode() == 200)) {
            return res;
        }
        return companyInfoService.updateRet(info);
    }

    /**
     * 客户唯一校验
     * @param info 客户属性值
     */
    @PostMapping("/checkInfo")
    public ResultUtil<T> checkInfo(@RequestBody CompanyInfo info) {
        return companyInfoService.checkInfo(info);
    }

    /**
     * 客户删除
     * @param id 客户id
     */
    @RequestMapping("/deleteInfo/{id}")
    @SysTemLog(operModul = "商务管理-客户管理", operType = "删除", operDesc = "客户删除功能", modular = "PC")
    public ResultUtil<T> deleteInfo(@PathVariable("id") Long id) {
        return companyInfoService.updateDelete(id);
    }


    /**
     * 根据id查询车队信息
     * @param vo
     * @return
     */
    @RequestMapping("/getCarTeamById")
    public ResultUtil<CompanyInfo> getCarTeamById(@RequestBody CompanyInfoVo vo) {
        CompanyInfo companyInfo = companyInfoService.getById(vo.getId());
        return ResultUtil.build(companyInfo, ResultUtilCodeEnum.SUCCESS);
    }

}
